<?php

include_once('HttpClient.class.php'); 
include_once('core.php'); 
$phonenumber = $_REQUEST["phone"];
if(empty($phonenumber)){return;};
if(strlen($phonenumber)!=11){return;};
GetProxy();

function GetProxy(){
	$http = new HttpClient("117.41.235.98",8888);
	$request = $http->quickGet("http://117.41.235.98:8888/getip.asp");
	if (empty($request))
	{
		echo false ;
		return;
	}
	
	$startPos = strpos($request,"QQ:1695834915<hr>")+17;
	$endPos = strrpos($request,"<br><hr>");
	$Proxy =trim( substr($request,$startPos,$endPos-$startPos),"\r\n");
	if (empty($Proxy))
	{
		echo false ;
		return;
	}
	$pos = strpos ($Proxy,":");
	$ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);
	if (empty($ProxyIP))
	{
		echo false ;
		return;
	}
	if ($ProxyPort<=0)
	{
		echo false ;
		return;
	}
	$bool = CheckProxy($ProxyIP,$ProxyPort);
	if ($bool)
	{
		Reg($ProxyIP,$ProxyPort);
	}
	else
	{
		GetProxy();
	}
}

function CheckProxy($ip,$port)
{
	$http = new HttpClient("wallet.tenpay.com");
	$http->SetProxy($ip,$port);
	$http->settimeout(5);

	$http->get("http://wallet.tenpay.com/cgi-bin/v1.0/queryqb.cgi");
	$request = $http->getContent();
	$string = new String();
	$result = $string->Between($request,"<ENV_ClientIp>","</ENV_ClientIp>");
	return $result == $ip;
}
function Reg($ip,$port)
{
	$obj = null;
	$arr = array();
	$string = new String();
	$url = "/reg?sid=AbtK89EGYpaJmAu0MSgAlNAZ&loginurl=http%3As%2F%2Fpt.3g.qq.com%2Fs%3Faid%3DnLoginnew%26q_from%3D3GQQ";
	$http = new HttpClient("pt.3g.qq.com");	
	$http->setUserAgent("Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; SLCC2)/UCWEB7.4.0.57/31/999");
	$http->setreferer("http://pt.3g.qq.com/s?aid=nLogin3gqq&auto=1&s_it=1&g_f=286&sid=AbtK89EGYpaJmAu0MSgAlNAZ");
	$http->get($url);
	$result = $http->getContent();
	$url = $string->Between($result,"go href=\"","\" method="); 
	if(strpos($url,"send_code.jsp?sid=") == false)
	{
		$obj -> code = -100;
		$obj -> msg ="解析地址出错";
		array_push($arr,$obj);
		echo json_encode($arr);
		return;
	}
	$pass = $string->randomkeys(10);
	$body ="nick={nick}&pwd={pass}&cellNum={telphone}&s_r=&loginurl=http%3a%2f%2fpt.3g.qq.com%2fs%3faid%3dnLoginnew%26q_from%3d3GQQ";
	$body = str_replace("{nick}",$string->randomkeys(10),$body);
	$body = str_replace("{pass}",$string->randomkeys(10),$body);
	$body = str_replace("{telphone}",$_REQUEST["phone"],$body);
	$http = new HttpClient("pt.3g.qq.com");
	$http->SetProxy($ip,$port);
	$http->settimeout(20);
	$http->setUserAgent("Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; SLCC2)/UCWEB7.4.0.57/31/999");
	$http->setreferer("http://pt.3g.qq.com/s?aid=nLogin3gqq&auto=1&s_it=1&g_f=286&sid=AbtK89EGYpaJmAu0MSgAlNAZ");
	$http->post($url,$body);
	$result = $http->getContent();

	if(strpos($result,"短信验证码已经成功下发"))
	{
		$obj -> code = 0;
		$obj -> msg ="OK";
		$obj -> sid = $string->Between($result,"pt.3g.qq.com/regsms/do_reg.jsp?","\" method=\"post\">");
		$body ="&s_r=&cellNum=".$_REQUEST["phone"]."&rsid=".$string->Between($result,"name=\"rsid\" value=\"","\"")."&loginurl=http://pt.3g.qq.com/s?aid=nLoginnew&amp;q_from=3GQ";
		$obj -> data = $body;
		
	}
	else if(strpos($result,"今天已经申请过"))
	{
		$obj -> code = -1;
		$obj -> msg ="已申请";
	}
	else if(strpos($result,"申请QQ受限"))
	{
		$obj -> code = -2;
		$obj -> msg ="申请受限";
	}
	else if(strpos($result,"验证码下发超限"))
	{
		$obj -> code = -3;
		$obj -> msg ="下发超限";
	}
	else 
	{
		echo $result;
		$obj -> code = -999;
		$obj -> msg ="异常错误";
	}
	array_push($arr,$obj);
	echo json_encode($arr);
}

?>